package com.stock_t.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stock_t.entity.StockPool;
import com.stock_t.page.TableDataInfo;
import com.stock_t.service.StockPoolService;
import com.stock_t.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 股票池控制器
 */
@Slf4j
@RestController
@RequestMapping("/stock/pool")
public class StockPoolController {

    @Autowired
    private StockPoolService stockPoolService;

    /**
     * 获取股票池列表
     */
    @PostMapping("/list")
    public Map<String, Object> list(@RequestBody StockPool stockPool) {
        // 如果使用QueryWrapper,要传入字符串  queryWrapper.orderByDesc("buy_shares");
        LambdaQueryWrapper<StockPool> queryWrapper = new LambdaQueryWrapper<>(stockPool);
        queryWrapper.orderByAsc(StockPool::getBuyPrice);
        List<StockPool> list = stockPoolService.list(queryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", list);
        result.put("msg", "success");
        return result;
    }
    
    /**
     * 分页查询股票池
     */
    @PostMapping("/page")
    public TableDataInfo page(@RequestBody StockPool stockPool) {
        // 使用PageUtils开启分页
        Page<StockPool> page = PageUtils.startPage();
        log.info("分页查询股票池，查询条件：{}", stockPool);
        // 构建查询条件
        QueryWrapper<StockPool> queryWrapper = new QueryWrapper<>(stockPool);
        stockPoolService.page(page, queryWrapper);
        
        return PageUtils.getDataTable(page);
    }
    
    /**
     * 根据ID获取股票池
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        StockPool stockPool = stockPoolService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (stockPool != null) {
            result.put("code", 200);
            result.put("data", stockPool);
            result.put("msg", "success");
        } else {
            result.put("code", 404);
            result.put("msg", "股票池不存在");
        }
        
        return result;
    }
    
    /**
     * 保存股票池（新增或更新）
     */
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody StockPool stockPool) {
        boolean success = stockPoolService.saveOrUpdate(stockPool);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "保存成功");
        } else {
            result.put("code", 500);
            result.put("msg", "保存失败");
        }
        
        return result;
    }
    
    /**
     * 更新股票池
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody StockPool stockPool) {
        // 确保记录存在
        StockPool existStockPool = stockPoolService.getById(stockPool.getId());
        
        Map<String, Object> result = new HashMap<>();
        if (existStockPool == null) {
            result.put("code", 404);
            result.put("msg", "股票池不存在");
            return result;
        }
        
        boolean success = stockPoolService.updateById(stockPool);
        
        if (success) {
            result.put("code", 200);
            result.put("msg", "更新成功");
        } else {
            result.put("code", 500);
            result.put("msg", "更新失败");
        }
        
        return result;
    }
    
    /**
     * 删除股票池
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable("id") Long id) {
        // 确保记录存在
        StockPool existStockPool = stockPoolService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (existStockPool == null) {
            result.put("code", 404);
            result.put("msg", "股票池不存在");
            return result;
        }
        
        boolean success = stockPoolService.removeById(id);
        
        if (success) {
            result.put("code", 200);
            result.put("msg", "删除成功");
        } else {
            result.put("code", 500);
            result.put("msg", "删除失败");
        }
        
        return result;
    }
    
    /**
     * 批量删除股票池
     */
    @DeleteMapping("/batch")
    public Map<String, Object> batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("msg", "请选择要删除的记录");
            return result;
        }
        
        boolean success = stockPoolService.removeByIds(ids);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "批量删除成功");
        } else {
            result.put("code", 500);
            result.put("msg", "批量删除失败");
        }
        
        return result;
    }
}